﻿using System;
using System.Configuration;
using IScheduler.Core.Properties;

namespace IScheduler.Core.Services
{
    public class SettingDotNetOpenMailData : ConfigurationSection
    {
        /// <summary>
        /// Looks up a localized string similar to SMTPPassword.
        /// </summary>
        [ConfigurationProperty("SMTPPassword")]
        public string SMTPPassword
        {
            get { return (string)this["SMTPPassword"]; }
            set { this["SMTPPassword"] = value; }
        }

        /// <summary>
        /// Looks up a localized string similar to SMTPUsername.
        /// </summary>
        [ConfigurationProperty("SMTPUsername")]
        public string SMTPUsername
        {
            get { return (string)this["SMTPUsername"]; }
            set { this["SMTPUsername"] = value; }
        }

        /// <summary>
        /// Looks up a localized string similar to SMTPServerPort.
        /// </summary>
        [ConfigurationProperty("SMTPServerPort")]
        public int SMTPServerPort
        {
            get { return int.Parse((string)this["SMTPServerPort"]); }

        }

        /// <summary>
        /// Looks up a localized string similar to SMTPAuthentication.
        /// </summary>
        [ConfigurationProperty("SMTPAuthentication")]
        public string SMTPAuthentication
        {
            get { return (string)this["SMTPAuthentication"]; }
        }

        /// <summary>
        /// Looks up a localized string similar to MailServer.
        /// </summary>
        [ConfigurationProperty("MailServer")]
        public string MailServer
        {
            get { return (string)this["MailServer"]; }
        }


        /// <summary>
        /// Looks up a localized string similar to MailSubject.
        /// </summary>
        [ConfigurationProperty("MailSubject")]
        public string MailSubject
        {
            get { return (string)this["MailSubject"]; }
        }

        /// <summary>
        /// Looks up a localized string similar to MailTo.
        /// </summary>
        [ConfigurationProperty("MailTo")]
        public string MailTo
        {
            get { return (string)this["MailTo"]; }
        }

        /// <summary>
        /// Looks up a localized string similar to MailFrom.
        /// </summary>
        [ConfigurationProperty("MailFrom")]
        public string MailFrom
        {
            get { return (string)this["MailFrom"]; }
        }

        /// <summary>
        /// Looks up a localized string similar to Mailer.
        /// </summary>
        [ConfigurationProperty("Mailer")]
        public Mailer MailerSmtp
        {
            get
            {
                /*
                string mailerToUse = (string)this["Mailer"];
                if (mailerToUse == null) mailerToUse = String.Empty;
                mailerToUse = mailerToUse.ToLower(System.Globalization.CultureInfo.InvariantCulture);

                switch (mailerToUse)
                {
                    default:
                        return new SmtpMailer();
                }
                 */

                return null;
            }
        }

        /// <summary>
        /// Looks up a localized string similar to CssForHtmlMail.
        /// </summary>
        [ConfigurationProperty("CssForHtmlMail")]
        public string CssForHtmlMail
        {
            get
            {

                // did they specify a name in the config file?
                string cssFile = (string)this["CssForHtmlMail"];

                // if nothing in config, default to HtmlMail.css
                if (cssFile == null || cssFile.Length == 0) cssFile = "HtmlMail.css";

                // if the file is found, return it, otherwise keep looking
                if (System.IO.File.Exists(cssFile)) return cssFile;

                // if the the path given is rooted, we can't probe any further
                if (!System.IO.Path.IsPathRooted(cssFile))
                {
                    // otherwise, take a look relative to the probing directory.
                    cssFile = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, cssFile);
                    if (System.IO.File.Exists(cssFile)) return cssFile;
                }

                throw new System.IO.FileNotFoundException(Resources.SettingDotNetOpenMailExceptionCssHtmlNotFound, cssFile);
            }
        }

        /// <summary>
        /// Looks up a localized string similar to CssForHtmlMail.
        /// </summary>
        [ConfigurationProperty("MailTextOnly")]
        public bool MailTextOnly
        {
            get { return bool.Parse((string)this["MailTextOnly"]); }
        }

    }
}
